<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="用户状态" prop="userStaus">-->
<!--        <el-input-->
<!--          v-model="queryParams.userStaus"-->
<!--          placeholder="请输入用户状态"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="用户状态" prop="userStaus">
        <el-select
          v-model="queryParams.userType"
          placeholder="Select"
          size="large"
          style="width: 240px"
        >
          <el-option
            v-for="item in userTypes"
            :label="item.label"
            :value="item.label"
          />
        </el-select>
      </el-form-item>


<!--      <el-form-item label="微信昵称" prop="nickName">-->
<!--        <el-input-->
<!--          v-model="queryParams.nickName"-->
<!--          placeholder="请输入微信昵称"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['system:userb:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userbList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="序号" align="center">-->
<!--        <template slot-scope="scope">-->
<!--          {{ scope.$index + 1 }} &lt;!&ndash; 使用 $index 获取当前行的索引，并加1以从1开始计数 &ndash;&gt;-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="用户类型 普通/vip" align="center" prop="userType" />
      <el-table-column label="用户状态  正常0、取消授权1、封禁2" align="center" prop="useState">
<!--        用户状态 正常0、取消授权1、封禁2-->
        <template slot-scope="scope">
          <el-tag v-if="scope.row.useState == 0" size="mini" type="success">正常</el-tag>
          <el-tag v-else-if="scope.row.useState == 1" size="mini" type="warning">取消授权</el-tag>
          <el-tag v-else-if="scope.row.useState == 2" size="mini" type="danger">封禁</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="微信昵称" align="center" prop="nickName" />
<!--      <el-table-column label="微信头像" align="center" prop="avatarUrl" >-->
<!--        <template slot-scope="scope">-->
<!--          <img :src="scope.row.avatarUrl" alt=""></img>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="消费金额" align="center" prop="spendingAmount" />
<!--      <el-table-column label="授权时间" align="center" prop="authDate" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.authDate, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="消费时间" align="center" prop="buyDate" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.buyDate, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="微信ID" align="center" prop="openId" />-->
<!--      <el-table-column label="微信key" align="center" prop="sessionKey" />-->
<!--      <el-table-column label="预留名称" align="center" prop="name" />-->
<!--      <el-table-column label="预留电话" align="center" prop="phone" />-->
      <el-table-column label="已提货" align="center" prop="orderNum0" >
        <template slot-scope="scope">
          <span style="cursor: pointer;color: #00afff" @click="userorder0(scope.row)">{{scope.row.orderNum0}}</span>
        </template>
      </el-table-column>
      <el-table-column label="未提货" align="center" prop="orderNum1" >
        <template slot-scope="scope">
          <span style="cursor: pointer;color: #00afff" @click="userorder1(scope.row)">{{scope.row.orderNum1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:userb:edit']"
          >编辑</el-button>
        </template>
      </el-table-column>
    </el-table>


<!--    <div class="demo-pagination-block">-->
<!--      <el-pagination-->
<!--        v-model:current-page="queryParams.pageNum"-->
<!--        v-model:page-size="queryParams.pageSize"-->
<!--        :total="total"-->
<!--        @size-change="handleSizeChangea"-->
<!--        @current-change="handleCurrentChangea"-->
<!--      />-->
<!--    </div>-->
    <div class="demo-pagination-block">
      <el-pagination
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[1]"
        :disabled="disabled"
        :background="background"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChangea"
        @current-change="handleCurrentChangea"
      />
    </div>


    <!-- 添加或修改userb对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="用户状态" prop="userStaus">-->
<!--          <el-input v-model="form.userStaus" placeholder="请输入用户状态  正常0、取消授权1、封禁2" />-->
<!--        </el-form-item>-->
        <el-form-item label="用户状态" prop="userStaus">
          <el-select
            v-model="form.useState"
            placeholder="Select"
            size="large"
            style="width: 240px"
          >
            <el-option
              v-for="item in options"
              :label="item.label"
              :value="item.userId"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listUserb, getUserb, delUserb, addUserb, updateUserb, routerSelect} from "@/api/system/userb";

export default {
  name: "Userb",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // userb表格数据
      userbList: [],
      // 0普通用户 1vip用户
      userTypes: [
        {
          userId: 0,
          label: "普通用户"
        },
        {
          userId: 1,
          label: "vip用户"
        }
      ],
      options: [
        {
          userId: 0,
          label: "正常"
        },
        {
          userId: 1,
          label: "取消授权"
        },
        {
          userId: 2,
          label: "封禁"
        }
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userType: null,
        userState: null,
        nickName: null,
        avatarUrl: null,
        spendingAmount: null,
        authDate: null,
        buyDate: null,
        openId: null,
        sessionKey: null,
        name: null,
        phone: null,
        password: null,
        userId:null,
        communityId:null,
        id:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.queryParams.id=this.$route.query.keyword
    this.queryParams.userId=this.$route.params.userId;
    this.queryParams.communityId=this.$route.params.CommunityId;
    // if(this.queryParams.userId==null){
    //   this.queryParams.userId=null
    // }
    if(this.queryParams.userId!=null || this.queryParams.communityId!=null){
      this.queryParams.pageNum=1;
      this.queryParams.pageSize=1000;
    }
    this.getList();
  },
  methods: {
    /** 查询userb列表 */
    getList() {
      if(this.queryParams.id==null){
        this.loading = true;
        listUserb(this.queryParams).then(response => {
          this.userbList = response.data.list;
          console.log(this.userbList+"aaaaaaaaaaaaa")
          this.total = response.data.total;
          this.loading = false;
          return false;
        });
      }else{
        this.loading = true;
        routerSelect(this.queryParams).then(res=>{
          this.userbList = res.list;
          this.total = res.total;
          this.loading = false;
        })
      }
    },

    handleSizeChangea(val) {
      this.queryParams.pageSize = val;
      this.getList(); // 假设这是你的数据获取函数
    },
    handleCurrentChangea(val) {
      this.queryParams.pageNum = val;
      this.getList(); // 同样地，假设这是你的数据获取函数
    },
    // 点击跳转到订单
    userorder0(row){
      this.$router.push({
        name: 'Ordera',
        params: {
          userId: row.userId,
          orderNum0: 0,
        }
      })
    },
    userorder1(row){
      this.$router.push({
        name: 'Ordera',
        params: {
          userId: row.userId,
          orderNum1: 1,
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: null,
        userType: null,
        userStaus: null,
        nickName: null,
        avatarUrl: null,
        spendingAmount: null,
        authDate: null,
        buyDate: null,
        openId: null,
        sessionKey: null,
        name: null,
        phone: null,
        password: null,
        orderNum0: null,
        orderNum1: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.resetForm("queryForm");
      // this.handleQuery();
      this.queryParams.userId=null;
      this.$route.params.userId=null;
      window.location.reload();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加userb";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const userId = row.userId || this.ids
      getUserb(userId).then(response => {
        this.form = response.data;
        console.log(response.data)
        this.open = true;
        this.title = "编辑用户："+response.data.nickName+"("+response.data.userId+")";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.userId != null) {
            updateUserb(this.form).then(response => {
              console.log(this.form)
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUserb(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids;
      this.$modal.confirm('是否确认删除userb编号为"' + userIds + '"的数据项？').then(function() {
        return delUserb(userIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/userb/export', {
        ...this.queryParams
      }, `userb_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
